package se.webbzon.boltzmann.minigame.sandbox;

import java.awt.Image;

import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;
import se.webbzon.oschi01.worldobject.WorldObject;

public class DefaultSandBoxContainer implements SandBoxContainer {
	
	// The 2-dimensional depth of this world object.
	private static final int depth = 100;
	
	// The container instance
	private final WorldObject container;
	
	// The image of this container
	private ResourceSet<Image> image;
	
	// The position of this world object
	private double x,y;
	
	// The content of this container
	private SandBoxItem content;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public DefaultSandBoxContainer() {
		container = new WorldContainer();
		image = Resources.getImageSet("minigame_sandbox_container");
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public boolean draggable() {
		return false;
	}

	@Override public boolean droppable() {
		return false;
	}
	
	@Override public void setPosition(double x, double y) {
		this.x = x;
		this.y = y;
	}

	@Override public double getX() {
		return x;
	}

	@Override public double getY() {
		return y;
	}

	@Override public double getWidth() {
		return 48;
	}

	@Override public double getHeight() {
		return 48;
	}

	@Override public boolean isEmpty() {
		return (content == null);
	}

	@Override public SandBoxItem getContent() {
		return content;
	}

	@Override public void setContent(SandBoxItem item) {
		if (item != null) {
			item.setPosition(getX(),getY());
		}
		this.content = item;
	}

	@Override public void onAttach(SandBox box) {
		box.getWorld().createInstance(container);
	}

	@Override public void onDetach(SandBox box) {
		box.getWorld().destroyInstance(container);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	/** The world object container. **/
	private final class WorldContainer extends WorldObject {

		public WorldContainer() {
			super(WorldObject.onPostDraw, depth);
		}
		
		@Override public void onPostDraw(WorldArea world, WorldCameraPane hud) {
			hud.drawImage(image.get(0), (int) (getX() - getWidth()/2), 
										(int) (getY() - getHeight()/2));
		}
		
	}
	

}
